<template>
  <div class="home-container">
    <!-- 欢迎区域 -->
    <div class="welcome-section" v-if="userStore.isLoggedIn">
      <div class="welcome-content">
        <div class="welcome-text">
          <h1>欢迎回来，{{ userStore.userInfo?.name }}！</h1>
          <p>开始您今天的学习之旅</p>
        </div>
        <div class="welcome-stats">
          <div class="stat-item">
            <el-icon size="24" color="#2c5aa0"><Document /></el-icon>
            <div class="stat-info">
              <div class="stat-value">{{ learningStats.inProgressCourses || 0 }}</div>
              <div class="stat-label">在学课程</div>
            </div>
          </div>
          <div class="stat-item">
            <el-icon size="24" color="#2c5aa0"><Clock /></el-icon>
            <div class="stat-info">
              <div class="stat-value">{{ learningStats.todayStudyTime || 0 }}分钟</div>
              <div class="stat-label">今日学习</div>
            </div>
          </div>
          <div class="stat-item">
            <el-icon size="24" color="#2c5aa0"><Trophy /></el-icon>
            <div class="stat-info">
              <div class="stat-value">{{ learningStats.achievements || 0 }}</div>
              <div class="stat-label">成就徽章</div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 轮播图 -->
    <div class="hero-section">
      <el-carousel height="400px" indicator-position="outside">
        <el-carousel-item v-for="item in banners" :key="item.id">
          <div class="banner-item" :style="{ backgroundImage: `url(${item.image})` }">
            <div class="banner-content">
              <h2>{{ item.title }}</h2>
              <p>{{ item.description }}</p>
              <el-button type="primary" size="large" @click="handleBannerClick(item)">
                {{ item.buttonText }}
              </el-button>
            </div>
          </div>
        </el-carousel-item>
      </el-carousel>
    </div>

    <!-- 特色功能 -->
    <div class="features-section">
      <div class="section-header">
        <h2>平台特色</h2>
        <p>专业的BIP在线学习平台，助您高效学习</p>
      </div>
      
      <div class="features-grid">
        <div class="feature-card">
          <div class="feature-icon">
            <el-icon size="48" color="#2c5aa0"><VideoPlay /></el-icon>
          </div>
          <h3>高清视频课程</h3>
          <p>专业录制的高清视频，支持倍速播放、字幕显示</p>
        </div>
        
        <div class="feature-card">
          <div class="feature-icon">
            <el-icon size="48" color="#2c5aa0"><Document /></el-icon>
          </div>
          <h3>在线考试系统</h3>
          <p>智能题库、自动评分、错题分析，学习效果看得见</p>
        </div>
        
        <div class="feature-card">
          <div class="feature-icon">
            <el-icon size="48" color="#2c5aa0"><TrendCharts /></el-icon>
          </div>
          <h3>学习进度跟踪</h3>
          <p>实时记录学习轨迹，生成个性化学习报告</p>
        </div>
        
        <div class="feature-card">
          <div class="feature-icon">
            <el-icon size="48" color="#2c5aa0"><ChatDotRound /></el-icon>
          </div>
          <h3>师生互动答疑</h3>
          <p>在线提问、实时答疑，解决学习中的疑问</p>
        </div>
      </div>
    </div>

    <!-- 推荐课程 -->
    <div class="recommended-section">
      <div class="section-header">
        <h2>推荐课程</h2>
        <el-button type="text" @click="$router.push('/courses')">
          查看全部 <el-icon><ArrowRight /></el-icon>
        </el-button>
      </div>
      
      <div class="courses-grid">
        <CourseCard
          v-for="course in recommendedCourses"
          :key="course.id"
          :course="course"
          @click="$router.push(`/courses/${course.id}`)"
        />
      </div>
    </div>

    <!-- 热门课程 -->
    <div class="popular-section">
      <div class="section-header">
        <h2>热门课程</h2>
        <el-button type="text" @click="$router.push('/courses')">
          查看全部 <el-icon><ArrowRight /></el-icon>
        </el-button>
      </div>
      
      <div class="courses-grid">
        <CourseCard
          v-for="course in popularCourses"
          :key="course.id"
          :course="course"
          @click="$router.push(`/courses/${course.id}`)"
        />
      </div>
    </div>

    <!-- 学习统计 -->
    <div class="stats-section" v-if="userStore.isLoggedIn">
      <div class="section-header">
        <h2>我的学习</h2>
      </div>
      
      <div class="stats-grid">
        <div class="stat-card">
          <h3>{{ learningStats.completedCourses || 0 }}</h3>
          <p>已完成课程</p>
        </div>
        <div class="stat-card">
          <h3>{{ learningStats.totalHours || 0 }}</h3>
          <p>学习时长(小时)</p>
        </div>
        <div class="stat-card">
          <h3>{{ learningStats.examScore || 0 }}</h3>
          <p>平均考试成绩</p>
        </div>
        <div class="stat-card">
          <h3>{{ learningStats.achievements || 0 }}</h3>
          <p>获得徽章</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { courseApi } from '@/api/course'
import { statisticsApi } from '@/api/exam'
import CourseCard from '@/components/CourseCard.vue'
import {
  VideoPlay,
  Document,
  TrendCharts,
  ChatDotRound,
  ArrowRight,
  Search,
  Clock,
  Trophy
} from '@element-plus/icons-vue'

const router = useRouter()
const userStore = useUserStore()

// 轮播图数据
const banners = ref([
  {
    id: 1,
    title: 'BIP系统实战课程',
    description: '从零开始学习BIP系统，掌握企业级应用开发技能',
    image: 'https://via.placeholder.com/1200x400/2c5aa0/ffffff?text=BIP+Course',
    buttonText: '立即学习',
    link: '/courses/1'
  },
  {
    id: 2,
    title: '在线认证考试',
    description: '参加BIP系统认证考试，获得权威技能证书',
    image: 'https://via.placeholder.com/1200x400/3a7bc8/ffffff?text=Exam',
    buttonText: '立即报名',
    link: '/exams'
  },
  {
    id: 3,
    title: '名师直播课堂',
    description: '与行业专家在线互动，实时解答学习疑问',
    image: 'https://via.placeholder.com/1200x400/4a8bd8/ffffff?text=Live',
    buttonText: '查看日程',
    link: '/live'
  }
])

// 推荐课程
const recommendedCourses = ref([])

// 热门课程
const popularCourses = ref([])

// 学习统计
const learningStats = ref({
  inProgressCourses: 3,
  todayStudyTime: 45,
  achievements: 5,
  completedCourses: 2,
  totalHours: 24,
  examScore: 85
})

// 加载推荐课程
const loadRecommendedCourses = async () => {
  try {
    const data = await courseApi.getRecommendedCourses()
    recommendedCourses.value = data.slice(0, 4)
  } catch (error) {
    console.error('加载推荐课程失败:', error)
  }
}

// 加载热门课程
const loadPopularCourses = async () => {
  try {
    const data = await courseApi.getPopularCourses()
    popularCourses.value = data.slice(0, 4)
  } catch (error) {
    console.error('加载热门课程失败:', error)
  }
}

// 加载学习统计
const loadLearningStats = async () => {
  if (!userStore.isLoggedIn) return
  
  try {
    const stats = await statisticsApi.getLearningStatistics()
    learningStats.value = stats
  } catch (error) {
    console.error('加载学习统计失败:', error)
  }
}

const handleBannerClick = (item) => {
  router.push(item.link)
}

onMounted(() => {
  loadRecommendedCourses()
  loadPopularCourses()
  loadLearningStats()
})
</script>

<style scoped>
.home-container {
  padding: 20px;
  background: #f0f8ff;
  min-height: calc(100vh - 140px);
}

.welcome-section {
  background: linear-gradient(to right, #2c5aa0, #4a8bd8);
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 30px;
  color: white;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.welcome-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 20px;
}

.welcome-text h1 {
  font-size: 24px;
  margin-bottom: 8px;
  color: white;
}

.welcome-text p {
  font-size: 16px;
  opacity: 0.9;
  margin: 0;
}

.welcome-stats {
  display: flex;
  gap: 30px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 10px;
}

.stat-info {
  display: flex;
  flex-direction: column;
}

.stat-value {
  font-size: 20px;
  font-weight: bold;
  color: white;
}

.stat-label {
  font-size: 14px;
  opacity: 0.9;
}

.hero-section {
  margin-bottom: 30px;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.banner-item {
  height: 100%;
  background-size: cover;
  background-position: center;
  display: flex;
  align-items: center;
  justify-content: center;
}

.banner-content {
  text-align: center;
  color: white;
  background: rgba(0, 0, 0, 0.5);
  padding: 40px;
  border-radius: 8px;
  max-width: 600px;
}

.banner-content h2 {
  font-size: 32px;
  margin-bottom: 16px;
}

.banner-content p {
  font-size: 18px;
  margin-bottom: 24px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.section-header h2 {
  color: #2c5aa0;
  font-size: 24px;
  margin: 0;
}

.features-section {
  margin-bottom: 40px;
  background: white;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.features-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 30px;
}

.feature-card {
  text-align: center;
  padding: 20px;
  border-radius: 8px;
  transition: all 0.3s;
  background: #f8fbff;
}

.feature-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 6px 16px rgba(44, 90, 160, 0.2);
}

.feature-icon {
  margin-bottom: 16px;
}

.feature-card h3 {
  color: #2c5aa0;
  margin-bottom: 12px;
}

.recommended-section,
.popular-section {
  margin-bottom: 40px;
  background: white;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.courses-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
}

.stats-section {
  background: white;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
}

.stat-card {
  text-align: center;
  padding: 20px;
  background: #f8fbff;
  border-radius: 8px;
  border-left: 4px solid #2c5aa0;
}

.stat-card h3 {
  color: #2c5aa0;
  font-size: 28px;
  margin-bottom: 8px;
}

@media (max-width: 768px) {
  .home-container {
    padding: 10px;
  }
  
  .welcome-content {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .welcome-stats {
    width: 100%;
    justify-content: space-between;
  }
  
  .banner-content {
    padding: 20px;
  }
  
  .banner-content h2 {
    font-size: 24px;
  }
  
  .banner-content p {
    font-size: 16px;
  }
  
  .features-grid,
  .courses-grid,
  .stats-grid {
    grid-template-columns: 1fr;
  }
}
</style>